package com.fuego.creditsystemcommodity.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuego.creditsystemcommodity.service.IOrganizerService;
import jakarta.annotation.Resource;
import org.example.dto.Result;
import org.example.entity.Organizer;
import org.example.utils.SystemConstants;
import org.springframework.web.bind.annotation.*;




@RestController
@RequestMapping("/organizer")
public class OrganizerController {

    @Resource
    public IOrganizerService organizerService;


    @GetMapping("/{id}")
    public Result queryOrganizerById(@PathVariable("id") Long id) {
        return organizerService.queryById(id);
    }

    @PostMapping
    public Result saveOrganizer(@RequestBody Organizer organizer) {
        // 写入数据库
        organizerService.save(organizer);
        // 返回店铺id
        return Result.ok(organizer.getId());
    }


    @PutMapping
    public Result updateOrganizer(@RequestBody Organizer organizer) {
        // 写入数据库
        organizerService.updateById(organizer);
        return organizerService.update(organizer);
    }


    @GetMapping("/of/type")
    public Result queryOrganizerByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        return organizerService.queryByType(typeId,current);
    }


    @GetMapping("/of/name")
    public Result queryOrganizerByName(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        // 根据类型分页查询
        Page<Organizer> page = organizerService.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 返回数据
        return Result.ok(page.getRecords());
    }

    @GetMapping("/list/allOrganizer")
    public Result queryAllOrganizer() {
        return organizerService.queryAllOrganizer();
    }
}
